using System;
using System.Collections.Generic;
using System.Text;
using AppExtensions.Routing;
using System.Web;

namespace AspNetExtensions.Routing
{
    public class BasePageRoute : Route
    {
        public BasePageRoute(string url)
        {
            this.url = url;
        }

        private string url;

        public string Url
        {
            get { return url; }
            set { url = value; }
        }
	
        public override void Redirect()
        {
            HttpContext.Current.Response.Redirect(this.url);
        }

        public override void Redirect(params KeyValuePair<string, string>[] parameterValues)
        {
            HttpContext context = HttpContext.Current;
            HttpServerUtility server = context.Server;

            if (parameterValues.Length > 0)
            {
                StringBuilder sb = new StringBuilder(url);
                bool isFirst = true;
                foreach (KeyValuePair<string, string> pair in parameterValues)
                {
                    if (isFirst)
                        sb.Append("?");
                    else
                        sb.Append("&");
                    sb.AppendFormat("{0}={1}", server.UrlEncode(pair.Key), server.UrlEncode(pair.Value));
                    isFirst = false;
                }
                url = sb.ToString();
            }
            context.Response.Redirect(url); 
        }
    }
}
